package br.gov.cemaden.cliente.precipitacao;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

/**
 *
 * @author Alan
 */
public class Reader {
    
    public Reader() {
    }
    
    public List<String> get(Path path) throws IOException{
        Long size = Files.size(path) / 1024;
        
        if(size > 8){
            return readBufferedFile(path);
        }
        return readFile(path);        
    }
    
    public List<String> get(InputStream stream) throws IOException{                
        List<String> lines = null;
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
        String line;
        while ((line = reader.readLine()) != null) {
            lines.add(line);
        }
        reader.close();
        return lines;
    }
    
    private List<String> readFile(Path path) throws IOException{
        return Files.readAllLines(path, StandardCharsets.ISO_8859_1);
    }
    
    private List<String> readBufferedFile(Path path) throws IOException{
        List<String> lines = null;
        BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.ISO_8859_1);
        String line;
        while((line = reader.readLine()) != null){
            lines.add(line);
        }
        reader.close();
        return lines;
    }
    
}
